package com.geek.novel.ui

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.*
import android.os.BatteryManager
import android.util.AttributeSet
import android.view.View
import com.geek.novel.R
import com.geek.novel.utils.CommUtil
import com.geek.novel.utils.DateUtil
import com.geek.novel.utils.LogCatUtil


/**
 * 电池，显示剩余电量
 */
class BatteryView : View {
	
	private var number = 1f
	
	private var strokeWidth = 0f
	private var padding = 0f
	private var drawPostTop = 0f
	private var drawPostLeft = 0f
	//电池极端的宽度
	private var capW = 0F
	//电池电量显示间距的大小
	private var capLR = 0F
	private var capTB = 0F
	//电池极端距离顶部底部的大小
	private var capPadding = 0F
	//电池电量文本大小
	private var numTextSize = 0F
	//时间文本大小
	private var timeTextSize = 0F
	//时间文本的左边距
	private var timeTextPaddingLeft = 0F

	var vWidth = 0f
	var vHeight = 0f

	private lateinit var intentFilter: IntentFilter
	private var broadcastReceiver = object : BroadcastReceiver(){
		override fun onReceive(context: Context?, intent: Intent?) {
			calcBattery(intent!!)
		}
	}

	/**销毁时调用注销方法*/
	fun onDestroy(){
		LogCatUtil.d(this::class.java.name, "销毁电池控件")
		context.unregisterReceiver(broadcastReceiver)
	}

	//颜色
	private var viewColor: Int = Color.BLACK
	private var viewBorderColor: Int = Color.BLACK
	fun setViewColor(color: Int, borderColor: Int){
		viewColor = color
		viewBorderColor = borderColor
	}
	
	private lateinit var paint: Paint

	constructor(context: Context): super(context){
		init(context, null, null)
	}
	constructor(context: Context, attrs: AttributeSet): super(context, attrs){
		init(context, attrs, null)
	}
	constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr){
		init(context, attrs, defStyleAttr)
	}

	private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int?){
		//电池边缘线的大小
		strokeWidth = resources.getDimension(R.dimen.d_1)
		
		padding = CommUtil.dip2px(1F)
		drawPostLeft = CommUtil.dip2px(2F)
		drawPostTop = CommUtil.dip2px(2F)
		//电池极端的宽度
		capW = CommUtil.dip2px(2F)
		//电池电量显示间距的大小
		capLR = CommUtil.dip2px(1F)
		capTB = CommUtil.dip2px(1F)
		//电池极端距离顶部底部的大小
		capPadding = CommUtil.dip2px(2F)
		//电池电量文本大小
		numTextSize = resources.getDimension(R.dimen.s_10)
		//时间文本大小
		timeTextSize = resources.getDimension(R.dimen.s_12)
		//时间文本的左边距
		timeTextPaddingLeft = resources.getDimension(R.dimen.d_6)

		paint = Paint()
		paint.strokeWidth = strokeWidth

		intentFilter = IntentFilter()
		intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED)

		context.registerReceiver(broadcastReceiver, intentFilter)

	}

	/**计算电量*/
	fun calcBattery(intent: Intent){
		val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
		val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1)
		number = level.toFloat() / scale
		LogCatUtil.d(this.javaClass.name, "电量改变:${number}")

		if(batteryChangeListener != null){
			batteryChangeListener!!.change(level, scale, number)
		}
	}
	
	override fun onDraw(canvas: Canvas) {
		drawView(canvas, offsetLeft, offsetTop)
	}

	private var offsetLeft = 0f
	private var offsetTop = 0f
	private var drawCanvas: Canvas? = null

	fun drawView(canvas: Canvas, posX: Float, posY: Float){
		drawCanvas = canvas
		offsetLeft = posX
		offsetTop = posY
		val left = 0F
		val top = 0F
		val width = vWidth
		val height = vHeight

		//保存画布状态
		canvas.save()

		canvas.translate(offsetLeft+drawPostLeft, offsetTop+drawPostTop)

		//边框颜色
		paint.color = viewBorderColor
		
		//绘制边框
		canvas.drawLine(left, top, width, top, paint)
		canvas.drawLine(left, top, left, height, paint)
		canvas.drawLine(width, top, width, height, paint)
		canvas.drawLine(left, height, width, height, paint)
		
		capLR = paint.strokeWidth

		//电池的电极
		canvas.drawRect(width+capLR, top + capPadding, width+capLR+capW, height - capPadding, paint)

		//内容的颜色
		paint.color = viewColor
		
		val paddingLR = capLR+padding
		
		//剩余电量
		val result = left+paddingLR + (width - paddingLR*2)*number
		
		canvas.drawRect(left+paddingLR, top+paddingLR, result, height-paddingLR, paint)

		//偏移量
		var textLeft = width

		//绘制电量文本
		//TODO
		
		//绘制时间
		paint.color = viewColor
		textLeft  += timeTextPaddingLeft
		paint.textSize = timeTextSize
		val time = DateUtil.getCurrentTime()
		//计算文本的baseline
		val fm = paint.fontMetrics
		val baseline = height/2 - (fm.bottom + fm.top)/2
		canvas.drawText(time, 0, time.length, textLeft, baseline, paint)
		
		canvas.restore()
	}

	interface BatteryChangeListener{
		fun change(level: Int, scale: Int, number: Float)
	}
	private var batteryChangeListener: BatteryChangeListener? = null
	fun setBatteryChangeListener(listener: BatteryChangeListener){
		batteryChangeListener = listener
	}

}